import pygame.font
from pygame.sprite import Group
from ship import Ship


class Scoreboard():
    def __init__(self,ai_settings,screen,stats):
        # 初始化显示得分的属性
        self.screen=screen
        self.screen_rect=screen.get_rect()# 获取最初的屏幕的类的属性赋值给得分属性
        self.ai_settings=ai_settings
        self.stats=stats

        # 显示得分时使用的字体设置
        # 设置文本颜色
        self.text_color = (30, 30, 30)
        # 设置字体的大小
        self.font = pygame.font.SysFont(None,48)

        # 准备显示分数的图像
        self.prep_score()   # 当前分数
        self.prep_high_score() # 最高分
        self.prep_level()  # 等级
        self.prep_ships() # 预备飞机

    def prep_score(self):
        # 将得分转换为一副渲染的图像
        # 将当前的得分四舍五入为最接近的十位数，并转化为证书
        round_score = int(round(self.stats.score, -1))
        # 将当前这串代码格式化为有千位分隔符的字符串
        score_str = "Score:{:,}".format_map(round_score)

        # 将得分放在屏幕右上角
        # 获得得分的位置
        self.score_rect = self.score_image.get_rect()
        # 是指得分位置
        self.score_rect.right = self.screen_rect.right - 20
        self.score_rect.top = 20

    def prep_high_score(self):
        """将最高得分转换为一幅渲染的图像"""
        # 将最高德丰同样是设置为十位数
        high_score = int(round(self.stats.high_score, -1))
        # 将其转化为有千分位的显示的字符串
        high_score_str = "High score:{:,}".format(high_score)
        # 设置最高得分的摆放位置
        self.high_score_image = self.font.render(high_score_str, True,
                                                 self.text_color, self.ai_settings.bg_color)

        # 将最高得分放在屏幕顶部中央
        self.high_score_rect = self.high_score_image.get_rect()
        self.high_score_rect.centerx = self.screen_rect.centerx
        self.high_score_rect.top = self.score_rect.top

    def prep_level(self):
        """将等级转换为渲染的图像"""
        # 按等级的字符串绘制，按照文本的颜色，以及统一设置的背景颜色
        self.level_image = self.font.render(str(self.stats.level), True,
                                            self.text_color, self.ai_settings.bg_color)

        # 将等级放在得分下面
        self.level_rect = self.level_image.get_rect()
        self.level_rect.right = self.score_rect.right
        self.level_rect.top = self.score_rect.bottom + 10

    def prep_ships(self):
        """显示剩余多少飞船."""
        self.ships = Group()
        for ship_number in range(self.stats.ships_left):
            ship = Ship(self.ai_settings, self.screen)
            ship.rect.x = 10 + ship_number * ship.rect.width
            ship.rect.y = 10
            self.ships.add_internal(ship)

    def prep_score():
        """在屏幕上显示飞船和得分"""
        self.screen.blit(self.score_image, self.score_rect)
        self.screen.blit(self.high_score_image, self.high_score_rect)
        self.screen.blit(self.level_image, self.level_rect)
        # 绘制飞船
        self.ships.draw(self.screen)  # 引入了ship的函数






